<template>
  <div class="dashboard-container">
    <div class="app-container">
      <PageTools>
        <template #after>
          <el-button type="primary" @click="addPermission('0',1)">添加权限</el-button>
        </template>
      </PageTools>
      <el-card>
        <el-table :data="data" row-key="id">
          <el-table-column label="菜单名称" prop="name" />
          <el-table-column label="权限标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="obj">
              <el-button v-if="obj.row.type===1" type="text" @click="addPermission(obj.row.id,2)">添加</el-button>
              <el-button type="text" @click="editPermission(obj.row.id)">查看</el-button>
              <el-button type="text" @click="delPermission(obj.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 放置一个弹层 用来编辑新增节点 -->
      <el-dialog :title="`${showText}权限点`" :visible.sync="showDialog" @close="btnCancel">
        <!-- 表单 -->
        <el-form ref="perForm" :model="formData" label-width="120px" :rules="rules">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" style="width:90%" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getAllpermission, getPermissionById, delAllpermission, updateAllpermission, addAllpermission } from '@/api/permission'
import { transformTreeData } from '@/utils/index'
export default {
  data() {
    return {
      data: [],
      formData: {},
      // 弹出层标题
      showText: '',
      showDialog: false,
      rules: {
        name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入权限标识', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getAllpermission()
  },
  methods: {
    // 获得所有权限列表
    async getAllpermission() {
      this.data = transformTreeData(await getAllpermission(), '0')
    },
    // 添加
    async addPermission(pid, type) {
      this.showText = '添加'
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
    },
    // 删除
    async delPermission(id) {
      try {
        await this.$confirm('确定删除嘛，是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning' })
        await delAllpermission(id)
        this.getAllpermission()
        this.$message.success('删除成功')
      } catch (err) {
        console.log(err)
      }
    },
    // 编辑
    async editPermission(id) {
      this.showText = '编辑'
      this.formData = await getPermissionById(id)
      this.showDialog = true
    },
    // 确定
    async btnOK() {
      try {
        await this.$refs.perForm.validate()
        if (this.formData.id) {
          await updateAllpermission(this.formData)
        } else {
          await addAllpermission(this.formData)
        }
        this.getAllpermission()
        this.$message.success(`${this.showText}成功`)
        this.showDialog = false
      } catch (err) {
        console.log(err)
      }
    },
    // 关闭弹层
    btnCancel() {
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      }
      this.$refs.perForm.resetFields()
    }

  }
}
</script>

<style>
</style>
